package com.codingday.app;

import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

import com.codingday.app.MyLocation.LocationResult;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;


public class TODOMapView extends MapActivity{

	static MapView googleMap;
	MapController mc;
	List<Overlay> mapOverlays;
	TODOMarker todoMarkerOverlay;
	MyLocation myLocation = null;
	LocationResult locationResult = null;
	//private MyLocationOverlay myLocationOverlay = null;
	
	//Criteria cta;
	//LocationManager locationManager;
	//String provider;
	//MyLocationManager myLocationManager = null;
	
	static TODOMapView instance;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		
		setContentView(R.layout.todomapview);
		
		googleMap = (MapView) findViewById(R.id.todomap_map);
		mc = googleMap.getController();


		initMapView();
		instance = this;
		Log.i("TODOMapView","OnCreate");
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		Log.i("TODOMapView","Resume---------------------------------");
		initMapView();
		//googleMap.invalidate();
		//initMapView();
		//myLocation.getLocation(this, locationResult, 10000);
		//ProgressDialog progressDialog = new ProgressDialog(this);
		final ProgressDialog pd = ProgressDialog.show(this, "",this.getString(R.string.map_waiting_msg));
		
		
		myLocation = new MyLocation();
		locationResult = new LocationResult(){

			@Override
			public void gotLocation(Location location) {
				// TODO Auto-generated method stub
				pd.dismiss();
				Log.i("TODOMapView","gotLocation");
				initMapPosition(location);
			}
			
		};
		myLocation.getLocation(this, locationResult, 10000);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		myLocation = null;
	}

	public static TODOMapView getInstance(){
		if(instance == null){
			instance = new TODOMapView();
		}
		return instance;
	}
	
	public void initMapView(){
		if(googleMap == null){
			return;
		}
		
		//initMapPosition();
		
		//googleMap.setSatellite(true);
		googleMap.setStreetView(true);
		googleMap.setBuiltInZoomControls(true);
		
		mapOverlays = googleMap.getOverlays(); 
		mapOverlays.clear();

		
		Drawable drawable = this.getResources().getDrawable(R.drawable.ping);
		todoMarkerOverlay = new TODOMarker(drawable,googleMap);

		for(int i = 0; i < TODOListManager.getTODOList().size() ;i++){
			//mapOverlays = googleMap.getOverlays(); 
			TODO todo = TODOListManager.getTODOList().get(i);
			mapOverlays.add(new OverlayCircle(todo));
			
			GeoPoint gp = new GeoPoint((int)(todo.getLatitude()*1E6), (int)(todo.getLongitude()*1E6));
			OverlayItem overylayItem = new OverlayItem(gp, todo.getTitle(),"hello");
			todoMarkerOverlay.addOverlay(overylayItem);
		}
		if(todoMarkerOverlay.size() >0){
			mapOverlays.add(todoMarkerOverlay);
		}
		
		/* TODO
		Location location = GeoAlarm.getInstance().getService().getMyLocation();
		Log.i("TODOMapView","initMapPosition : "+location.getLatitude()+","+location.getLongitude());
		mapOverlays.add(new OverlayYou(this,location.getLatitude(),location.getLongitude()));
		*/
		Location location = GeoAlarm.getInstance().getService().getLastKnownLocation();
		if(location != null){
			GeoPoint p = new GeoPoint(((int)(location.getLatitude()*1E6)), ((int)(location.getLongitude()*1E6)));
			mc.animateTo(p);
		}
	}
	
	
	public void initMapPosition(Location location){

		
		try {
			Looper.prepare();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		Log.i("initMapPosition","Found your Location!");
		if(location == null){
			Log.i("TODOMapView","initMapPosition : location is null");
		}
		GeoPoint p = new GeoPoint(((int)(location.getLatitude()*1E6)), ((int)(location.getLongitude()*1E6)));
		mapOverlays.add(new OverlayYou(this,location.getLatitude(),location.getLongitude()));
		mc.animateTo(p);
		mc.setZoom(16);
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	public class OverlayCircle extends Overlay{
		
		

		TODO todo;
		public OverlayCircle(TODO todo){
			this.todo = todo;
		}
		
		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);
			 Paint paint1 = new Paint();
		     Paint paint2 = new Paint();
		     Paint stroke = new Paint();

		     paint1.setARGB(30, 200, 0, 255); // a, r, g, b
		     /*
		     paint2.setARGB(20, 255, 255, 255);
		     paint2.setStyle(Style.FILL);
		     canvas.drawPaint(paint2);
		     */
		     paint2.setARGB(255, 189, 222,0);
		     paint2.setTextAlign(Paint.Align.CENTER);
		     paint2.setTextSize(20);
		     paint2.setTypeface(Typeface.DEFAULT_BOLD);
		     paint2.setStrokeWidth(2);
		     
		     stroke.setARGB(255, 0, 0,0);
		     stroke.setTextAlign(Paint.Align.CENTER);
		     stroke.setTextSize(20);
		     stroke.setTypeface(Typeface.DEFAULT_BOLD);
		     stroke.setStyle(Paint.Style.STROKE);
		     stroke.setStrokeWidth(7);
		     
		     GeoPoint geoPoint = new GeoPoint((int)(todo.getLatitude()*1E6), (int)(todo.getLongitude()*1E6)); 
		     Point pixPoint = new Point(); 
		     mapView.getProjection().toPixels(geoPoint, pixPoint); 
		     
		     int radiusR = metersToRadius(todo.getRadius(), mapView, todo.getLatitude());
		     canvas.drawCircle(pixPoint.x, pixPoint.y, radiusR, paint1);
		    
		     
		     //Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ping);
		     //canvas.drawBitmap(bmp, pixPoint.x-bmp.getWidth()/2, pixPoint.y-bmp.getHeight()/2, null);   
		     //canvas.drawBitmap(bmp, pixPoint.x-bmp.getWidth()/2, pixPoint.y-bmp.getHeight(), null); 
		     
		     
		     
		     canvas.drawText(todo.getTitle(), pixPoint.x, pixPoint.y+20, stroke);
		     canvas.drawText(todo.getTitle(), pixPoint.x, pixPoint.y+20, paint2);
		     
		}
	}

	private static int metersToRadius(float meters, MapView map, double latitude) {
	    return (int) (map.getProjection().metersToEquatorPixels(meters) * (1/ Math.cos(Math.toRadians(latitude))));         
	}
	
	public void goToViewToDoActivity(long ID){
		Intent intent = new Intent(this, TODOView.class);
		//intent.putExtra("TODOID", ID);
		intent.putExtra("TODOID", ID);
		Log.i("TODOListView","goToViewToDoActivity - ID:"+ID);
		startActivityForResult(intent,2);
	}	
	

}
